package cn.bdqn.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import cn.bdqn.bean.Bill;
import cn.bdqn.bean.Provider;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.BillDao;
import cn.bdqn.dao.ProviderDao;
import cn.bdqn.dao.impl.BillDaoImpl;
import cn.bdqn.dao.impl.ProviderDaoImpl;
import cn.bdqn.service.ProviderService;

public class ProviderServiceImpl implements ProviderService {
	private ProviderDao providerDao = null;
	private BillDao billDao = null; 
	public ProviderServiceImpl(){
		providerDao = new ProviderDaoImpl();
		billDao = new BillDaoImpl();
	}

	/**
	 * 增加供应商
	 */
	@Override
	public boolean addProvider(Provider provider) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		//开启事务
		try {
			con.setAutoCommit(false);
			row = providerDao.addProvider(con, provider);
			//提交事务
			con.commit();
		} catch (SQLException e) {
			try {
				//有异常回滚事务
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		if(row>0){
			System.out.println("add success");
			return true;
		}else{
			System.out.println("add failure");
			return false;
		}
	}

	/**
	 * 修改供应商
	 */
	@Override
	public boolean updateProvider(Provider provider) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		
		try {
			//开启事务
			con.setAutoCommit(false);
			//删除provider时先要在bill表里查找有没有该供应商下的账单，如果有，不能删除，提示先删除bill
			//根据id
			row = providerDao.updateProvider(con, provider);
			//提交事务
			con.commit();
		} catch (SQLException e) {
			try {
				//有异常回滚事务
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		if(row>0){
			System.out.println("update success");
			return true;
		}else{
			System.out.println("update failure");
			return false;
		}
	}

	/**
	 * 删除供应商
	 * 逻辑删除
	 */
	@Override
	public boolean deleteProvider(Provider provider) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		Bill bill = new Bill();
		bill.setProviderId(provider.getId());
		try {
			if(billDao.getCount(con, bill) > 0){
				//有，则不能删除
				System.out.println("该供应商名下有订单记录，不能删除！");
				return false;
			}else{
				//开启事务
				con.setAutoCommit(false);
				//删除provider时先要在bill表里查找有没有该供应商下的账单，如果有，不能删除，提示先删除bill
				//根据id
				row = providerDao.deleteProvider(con, provider);
				//提交事务
				con.commit();
			}
		} catch (SQLException e) {
			try {
				//有异常回滚事务
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		if(row>0){
			System.out.println("delete success");
			return true;
		}else{
			System.out.println("delete failure");
			return false;
		}
	}

	/**
	 * 获取总记录数
	 */
	@Override
	public int getCount(Provider provider) {
		int count = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			count = providerDao.getCount(con, provider);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return count;
	}

	/**
	 * 获取带有查询条件的分页列表
	 */
	@Override
	public List<Provider> getList(Provider provider, int pageIndex, int pageSize) {
		List<Provider> list = null;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			list = providerDao.getList(con, provider, pageIndex, pageSize);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return list;
	}

	/**
	 * 根据id获取供应商的所有信息
	 */
	@Override
	public Provider getProviderById(int id) {
		Provider provider = null;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			provider = providerDao.getProviderById(con, id);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return provider;
	}

	/**
	 * 获取总列表
	 */
	@Override
	public List<Provider> getList() {
		List<Provider> list = null;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			list = providerDao.getList(con);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return list;
	}

}
